package panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import file.DB;
import file.IOStreamStudent;
import model.Student;

public class EditPanel extends JFrame implements ActionListener{
	public static EditPanel instance=new EditPanel();
	JLabel name=new JLabel("姓名");
	JLabel xuehao=new JLabel("学号");
	JLabel ceyan=new JLabel("测验");
	JLabel kaoqin=new JLabel("考勤");
	JLabel qimo=new JLabel("期末");
	JLabel zuoye=new JLabel("作业");
	JLabel zongpin=new JLabel("总评");
	JLabel kecheng=new JLabel("课程");
	JLabel laoshi=new JLabel("老师");
	JLabel banji=new JLabel("班级");
	JTextField tfName=new JTextField(15);
	JTextField tfXuehao=new JTextField(15);
	JTextField tfCeyan=new JTextField(15);
	JTextField tfKaoqin=new JTextField(15);
	JTextField tfQimo=new JTextField(15);
	JTextField tfZuoye=new JTextField(15);
	JTextField tfZongpin=new JTextField(15);
	JButton addStu=new JButton("修改");
	JTextField tfCourse=new JTextField(15);
	JTextField tfTeacher=new JTextField(15);
	JTextField tfStu_class=new JTextField(15);
	String labelKecheng=ScorePage.instance.box.getSelectedItem().toString();
	String labelTeacher=ScorePage.instance.box2.getSelectedItem().toString();
	String labelBanji=ScorePage.instance.box3.getSelectedItem().toString();
	JPanel panel=new JPanel();
	public EditPanel(){
		this.setTitle("修改学生信息");
		this.setSize(230, 300);
		panel.add(xuehao);
		panel.add(tfXuehao);
		panel.add(name);
		panel.add(tfName);
		panel.add(kaoqin);
		panel.add(tfKaoqin);
		panel.add(ceyan);
		panel.add(tfCeyan);
		panel.add(zuoye);
		panel.add(tfZuoye);
		panel.add(qimo);
		panel.add(tfQimo);
		panel.add(zongpin);
		panel.add(tfZongpin);
		panel.add(addStu);
//		panel.add(kecheng);
//		panel.add(tfCourse);
//		panel.add(laoshi);
//		panel.add(tfTeacher);		
//		panel.add(banji);
//		panel.add(tfStu_class);		
		this.add(panel);
		
		addStu.addActionListener(this);
		this.setLocationRelativeTo(null);//位于屏幕中央
		this.setResizable(false);//设置此窗体是否可由用户调整大小
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭操作
	}
	public static void main(String[] args){
		EditPanel.instance.setVisible(true);
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String xuehao=tfXuehao.getText();
		String name=tfName.getText();
		String ceyan=tfCeyan.getText();
		String kaoqin=tfKaoqin.getText();
		String qimo=tfQimo.getText();
		String zongpin=tfZongpin.getText();
		String zuoye=tfZuoye.getText();
		String course=tfCourse.getText();
		String teacher=tfTeacher.getText();
		String stu_class=tfStu_class.getText();
		JButton button=(JButton) e.getSource();
		if(button==this.addStu){
			int flag=JOptionPane.showConfirmDialog(this, "是否修改？");
			if(flag==JOptionPane.YES_OPTION){
				DB.arrStu=IOStreamStudent.readFromFile();
				Iterator<Student> it = DB.arrStu.iterator();//next()方法不能在循环内使用2次
		        while(it.hasNext()){
		        	Student student=it.next();
		        	if(student.getXueHao().equals(xuehao)){
		        		student.setCeYan(ceyan);
		        		student.setKaoQing(kaoqin);
		        		student.setQiMo(qimo);
		        		student.setZongJi(zongpin);
		        		student.setZuoYe(zuoye);	
		        	}
		        IOStreamStudent.writeToFile();
				JOptionPane.showMessageDialog(this, "修改成功！");
				AddPanel.instance.setVisible(false);
			}
		}

	}
	}
}
